package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    /**
     * 查看用户所有订单
     */

    @Override
    public Map<String, Object> queryOrder(OrderListRequest orderListRequest) {
        PageHelper.startPage(orderListRequest.getPage(),orderListRequest.getSize());

        List<OrderDetailInfo> orders = orderMapper.selectOrderByUserId(orderListRequest.getUserId());
        long total = new PageInfo<>(orders).getTotal();
//        Long  = (long) orders.size();
       for (OrderDetailInfo order : orders) {
            List<OrderItemDto> orderItemDto = orderMapper.selectOrderItemByOrderId(order.getOrderId());
            OrderShippingDto orderShippingDto = orderMapper.selectOrderShippingByOrderId(order.getOrderId());
            order.setOrderItemDto(orderItemDto);
            order.setOrderShippingDto(orderShippingDto);
        }


        Map<String,Object> result = new HashMap<>();
        result.put("data",orders);
        result.put("total",total);
        return result;
    }
}
